const fs = require('fs');
const { yuedun, sinablog, csdn, juejin, gongzhonghao } = require('./config.json');

/**
 * 自动写博客类
 */
class WriteBlog {
    // 月盾
    async yuedunBlog(page) {
        // 登录
        await page.goto("http://www.yuedun.wang/admin/login");
        await page.type('input[name=username]', yuedun.uname);
        await page.type('input[name=password]', yuedun.pwd);
        await page.click('.btn');

        // 写博客
        await page.goto('http://www.yuedun.wang/admin/newArticleMd');
        let { title, file } = await this.readBlog();
        await page.type('#title_color', title);
        await page.waitFor(5000);
        await page.click('.CodeMirror-lines');
        await page.type('.CodeMirror-lines', file);
        await page.type('input[name=category]', '随笔');
        await page.select('select[name=status]', '0');
        // await page.click('input[type=submit]');

        const blogHtml = await page.$eval('.markdown-body.editormd-preview-container', el => el.innerHTML);
        return {
            title,
            blogHtml
        };
    }

    //新浪博客
    async sinaBlog(page) {
        await page.goto('http://blog.sina.com.cn/u/1704122375');
        await page.waitForSelector('.login');
        await page.click('.login');
        await page.type('#login_name_d', sinablog.uname);
        await page.type('#login_pass_d', sinablog.pwd);
        await page.click('#login_button');
        await page.waitForSelector('#SG_Publish')
        await page.goto('http://control.blog.sina.com.cn/admin/article/article_add.php');
        // await page.waitForSelector('.guide_btn');
        // await page.click('.guide_btn');
        await page.type('#articleTitle', title);
        await page.click('#SinaEditor_59_viewcodecheckbox');
        await page.type('#SinaEditorTextarea', bloghtml);
        await page.click('#SinaEditor_59_viewcodecheckbox');
        await page.click('#articlePosTempBtn');
        return true;
    }
    //CSDN博客
    async csdnBlog(page) {
        // 登录
        await page.goto('https://passport.csdn.net/login?code=public');
        await page.click('#app > div > div > div.main > div.main-login > div.main-select > ul > li:nth-child(2) > a');
        await page.waitForSelector('#all');
        await page.type('#all', csdn.uname);
        await page.type('#password-number', csdn.pwd);
        await page.click('#app > div > div > div.main > div.main-login > div.main-process-login > div > div:nth-child(6) > div > button');
        await page.waitForSelector('.hasAvatar');
        // 写博客
        let { title, file } = await this.readBlog();
        await page.goto('https://editor.csdn.net/md/?not_checkout=1');
        await page.evaluate(() => document.querySelector(".article-bar__title.article-bar__title--input.text-input").value = "");
        await page.type('.article-bar__title.article-bar__title--input.text-input', title);
        //清空原有内容
        await page.evaluate(() => {
            document.querySelector('.editor__inner.markdown-highlighting').innerHTML = "";
        })
        await page.click('.editor');
        await page.type('.editor', file);
        return true;
    }
    // 掘金
    async juejinBlog(page) {
        // 登录
        await page.goto('https://juejin.cn/');
        await page.waitForSelector('.login-button');
        await page.click('.login-button');
        await page.click('.clickable');
        await page.type('input[name=loginPhoneOrEmail]', juejin.uname);
        await page.type('input[name=loginPassword]', juejin.pwd);
        await page.click('#juejin > div.global-component-box > div.auth-modal-box > form > div.panel > button');
        await page.waitForSelector('.lazy.avatar.avatar.immediate');
        // 写博客
        let { title, file } = await this.readBlog();
        await page.waitForSelector('.add-btn');
        await page.click('.add-btn');
        await page.waitForSelector('.title-input');
        await page.type('.title-input', title);
        await page.mouse.click(200, 300);
        await page.type('.CodeMirror-lines', file);
        return true;
    }
    // 公众号
    async gongzhonghao(page, browser, title, bloghtml) {
        // 登录
        try {
            await page.goto('https://mp.weixin.qq.com/');
            await page.waitForSelector('.login__type__container__select-type');
            await page.click('#header > div.banner > div > div > div.login__type__container.login__type__container__scan > a');
            await page.type('input[name=account]', gongzhonghao.uname);
            await page.type('input[name=password]', gongzhonghao.pwd);
            await page.click('.btn_login');
            await page.waitForSelector('.weui-desktop-account__thumb');
            await page.waitForSelector('.new-creation__menu-item:nth-child(1)');
            await page.click('.new-creation__menu-item:nth-child(1)');
            // 写博客
            await browser.on('targetcreated', async (target) => {
                const newPage = await target.page();
                // 标题
                await newPage.waitForSelector('#title');
                await newPage.type('#title', title);
                // 内容
                const frame = newPage.frames().find(frame => {
                    console.log(">>>>>>>>>>>>>frame-name", frame.name());
                    return true;
                });
                console.log(">>>>>fram-url", frame.url());
                await frame.waitForSelector('.view');
                await frame.click('.view');// TODO
                await frame.type('.view', bloghtml);
            })
            return true;
        } catch (error) {
            console.error(error);
        }
    }

    // 读取文件
    readBlog() {
        let file = null;
        let title = '';
        try {
            file = fs.readFileSync('./blog.md', 'utf-8');
            let index = file.indexOf('\n');
            title = file.substring(0, index);
        } catch (error) {
            console.error(error);
        }
        return {
            title,
            file
        };
    }
}

module.exports = WriteBlog;